package com.ccrfid.rmc6.service.personnel;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.appconfig.exception.pojo.CustomErrors;
import com.ccrfid.rmc6.entity.personnel.PersonnelMutualGroupEntity;
import com.ccrfid.rmc6.entity.personnel.PersonnelMutualGroupMemberEntity;
import com.ccrfid.rmc6.repository.personnel.PersonnelMutualGroupMemberRepository;
import com.ccrfid.rmc6.repository.personnel.PersonnelMutualGroupRepository;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.transaction.Transactional;
import javax.ws.rs.core.Response;
import java.util.List;

@Service
@Transactional
public class MutualGroupServiceImpl implements MutualGroupService {
    private static final Logger logger = LoggerFactory.getLogger(MutualGroupServiceImpl.class);
    @Autowired
    private PersonnelMutualGroupRepository mutualGroupRepository;
    @Autowired
    private PersonnelMutualGroupMemberRepository mutualGroupMemberRepository;

    @Override
    public Page<PersonnelMutualGroupEntity> getPersonnelMutualGroupPage(SearchCriteria criteria, Pageable pageable) {
        return mutualGroupRepository.search(criteria, pageable);
    }

    @Override
    public PersonnelMutualGroupEntity createPersonnelMutualGroup(PersonnelMutualGroupEntity entity)
            throws AppException {
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.mutual_group_name_is_null.getCode(), CustomErrors.mutual_group_name_is_null.getReason(), "");
        }
        return mutualGroupRepository.save(entity);
    }

    @Override
    public PersonnelMutualGroupEntity updatePersonnelMutualGroup(Long groupId, PersonnelMutualGroupEntity entity)
            throws AppException {
        if (!mutualGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (StringUtils.isEmpty(entity.getGroupName())) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.mutual_group_name_is_null.getCode(), CustomErrors.mutual_group_name_is_null.getReason(), "");
        }
        PersonnelMutualGroupEntity groupEntity = mutualGroupRepository.getById(groupId);
        groupEntity.setGroupType(entity.getGroupType());
        groupEntity.setGroupName(entity.getGroupName());
        groupEntity.setRemark(entity.getRemark());
        return mutualGroupRepository.save(groupEntity);
    }

    @Override
    public Boolean deletePersonnelMutualGroup(Long groupId) throws AppException {
        if (!mutualGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        //1.删除互监组相关成员
        if (mutualGroupMemberRepository.existsByGroupId(groupId)) {
            mutualGroupMemberRepository.deleteAllByGroupId(groupId);
        }
        //2.删除互监组
        mutualGroupRepository.deleteById(groupId);
        return true;
    }

    @Override
    public List<PersonnelMutualGroupMemberEntity> getPersonnelMutualGroupMemberList(Long groupId) {
        return mutualGroupMemberRepository.getAllByGroupId(groupId);
    }

    @Override
    public PersonnelMutualGroupMemberEntity createPersonnelMutualGroupMember(Long groupId,
                                                                             PersonnelMutualGroupMemberEntity entity)
            throws AppException {
        if (!mutualGroupRepository.existsById(groupId)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        if (mutualGroupMemberRepository.existsByGroupIdAndPersonnelId(groupId, entity.getPersonnelId())) {
            return mutualGroupMemberRepository.getByGroupIdAndPersonnelId(groupId, entity.getPersonnelId());
        } else {
            entity.setId(null);
            entity.setGroupId(groupId);
            return mutualGroupMemberRepository.save(entity);
        }
    }

    @Override
    public Boolean deletePersonnelMutualGroupMember(Long groupId, Long memberId) throws AppException {
        if (!mutualGroupRepository.existsById(groupId)
                || !mutualGroupMemberRepository.existsByIdAndGroupId(memberId, groupId)) {
            throw new AppException(Response.Status.BAD_REQUEST.getStatusCode(),
                    CustomErrors.resource_not_found.getCode(), CustomErrors.resource_not_found.getReason(), "");
        }
        mutualGroupMemberRepository.deleteByIdAndGroupId(memberId, groupId);
        return true;
    }
}
